#-*- coding:utf-8 -*-
__author__ = 'jslijb'
from tornado import web,httpserver,ioloop
from create_qr_code import get_code_by_str
SIGN_FILE_HANDLE = open("sing.csv","a",encoding="GBK")
SIGN_FILE_HANDLE.write("姓名,部门,工号\n")
class IndexPageHandler(web.RequestHandler):
    def get(self,*args,**kwargs):
        #self.write("hello welcom to python")
        self.render("index.html")

class CodeHandler(web.RequestHandler):
    def get(self,*args,**kwargs):
        #self.write("hello welcom to python")
        # self.render("index.html")
        img_handler = get_code_by_str("hello well come python")
        self.write(img_handler.getvalue())

class SignHandler(web.RequestHandler):
    def get(self,*args,**kwargs):
        self.render("sign.html")

    def post(self, *args, **kwargs):
        name = self.get_argument("name")
        department = self.get_argument("department")
        num = self.get_argument("num")
        if name and department and num:
            SIGN_FILE_HANDLE.write('%s,%s,%s\n' %(name, department, num))
            SIGN_FILE_HANDLE.flush()
            self.write("签到成功")
        else:
            self.write("请填写完整的信息")

        # print("name=",name,"department=",department,"num=",num)
application = web.Application([
    (r"/index", IndexPageHandler),
    (r"/get_code", CodeHandler),
    (r"/sign", SignHandler),
])



if __name__ == '__main__':
        http_server = httpserver.HTTPServer(application)
        http_server.listen(8080)
        ioloop.IOLoop.current().start()
